﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace LocalizationLibrary.Reflection
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    
    //LocalizationLibrary References
    using LocalizationLibrary.Attributes;

    /// <summary>
    /// Implements a wrapper class of the class <see cref="Assembly"/>.
    /// </summary>
    internal class AssemblyRef
    {
        //Fields

        private DynamicResourcePartAttribute[] _resourceLocalizedAttribute;

        //Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="assembly">Assembly.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown whether <paramref name="assembly"/> parameter is null.
        /// </exception>
        public AssemblyRef(Assembly assembly)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly parameter shouldn't be null.");
            }
            this.Assembly = assembly;
        }

        //Properties

        /// <summary>
        /// Gets wrapped assembly.
        /// </summary>
        public Assembly Assembly { get; private set; }

        /// <summary>
        /// Gets an array that contains the <paramref name="ResourceLocalizedAttribute"/>s defined in the wrapped assembly.
        /// </summary>
        public DynamicResourcePartAttribute[] ResourceLocalizedAttribute
        {
            get
            {
                if (this._resourceLocalizedAttribute == null)
                {
                    this._resourceLocalizedAttribute =
                        this.Assembly.GetCustomAttributes(typeof(DynamicResourcePartAttribute), true) as DynamicResourcePartAttribute[];
                    if (this._resourceLocalizedAttribute == null)
                    {
                        this._resourceLocalizedAttribute = new DynamicResourcePartAttribute[0];
                    }
                }
                return _resourceLocalizedAttribute;
            }
        }

        //Methods

        /// <summary>
        /// Serves as a hash function for <see cref="AssemblyRef"/> type.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Assembly.FullName.GetHashCode();
        }

        /// <summary>
        /// Determines whether the specified <see cref="AssemblyRef"/> is equals to the current <see cref="AssemblyRef"/>.
        /// </summary>
        /// <param name="obj">Instance.</param>
        /// <returns>True whether the input instance is equals to the current instance otherwise False.</returns>
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }
            else if ((obj == null) || (obj.GetType() != this.GetType()))
            {
                return false;
            }
            AssemblyRef instance = obj as AssemblyRef;
            return instance.Assembly.FullName.Equals(this.Assembly.FullName);
        }
    }
}